package wnhz.com.business.freeshop.activity.fristfree;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.groupshop.adapter.QuanZiImageAdapter;
import wnhz.com.business.groupshop.bean.ShareInfo;
import wnhz.com.business.groupshop.ui.AddCartActivity;
import wnhz.com.business.groupshop.ui.AllCommentActivity;
import wnhz.com.business.groupshop.ui.StoreActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.dialog.ShareDialog;
import wnhz.com.business.view.CircleImageView;
import wnhz.com.business.view.ExpandTextView;
import wnhz.com.business.view.NoScrollGridView;
import wnhz.com.business.view.TranslucentActionBarW;

/**
 * 商品详情
 */
public class FreeGoodsDetailsActivity extends MyBaseActivity implements View.OnClickListener, ActionBarClickListener {
    private static final String TAG = "RebateGoodsDetailsActivity";

    @BindView(R.id.tv_collect)
    TextView tv_collect;
    @BindView(R.id.tv_sales)
    TextView tv_sales;
    @BindView(R.id.ll_allComment)
    LinearLayout ll_allComment;
    @BindView(R.id.tv_comment)
    TextView tv_comment;
    @BindView(R.id.iv_userImg)
    CircleImageView iv_userImg;
    @BindView(R.id.tv_userName)
    TextView tv_userName;
    @BindView(R.id.tv_userComment)
    ExpandTextView tv_userComment;
    @BindView(R.id.tv_commentTime)
    TextView tv_commentTime;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.ll_collect)
    LinearLayout ll_collect;
    @BindView(R.id.iv_collect)
    ImageView iv_collect;
    @BindView(R.id.tv_collect_status)
    TextView tv_collect_status;
    @BindView(R.id.lay)
    LinearLayout lay;
    @BindView(R.id.ll_have_comment)
    LinearLayout ll_have_comment;
    @BindView(R.id.ll_no_comment)
    LinearLayout ll_no_comment;

    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    @BindView(R.id.gridview)
    NoScrollGridView gridview;
    @BindView(R.id.actionbar)
    TranslucentActionBarW actionbar;

    @BindView(R.id.ll_buy)
    LinearLayout ll_buy;
    @BindView(R.id.ll_pin)
    LinearLayout ll_pin;

    @BindView(R.id.tv_oldPrice)
    TextView tv_oldPrice;
    @BindView(R.id.tv_look)
    TextView tvLook;

    private ArrayList<String> imageUrls = new ArrayList<>();
    private boolean isexpand;
    private String token;
    private boolean isCllected = false;
    private Map<String, Object> map = new HashMap<>();
    private String goodsId;
    private String logo_pic;
    private String FromType;
    private List<String> list = new ArrayList<>();
    private ShareDialog shareDialog;


    public static Intent createIntent(Context context, String goodsId, String logo_pic, String FromType) {
        Intent intent = new Intent(context, FreeGoodsDetailsActivity.class);
        intent.putExtra("goodsId", goodsId);
        intent.putExtra("logo_pic", logo_pic);
        intent.putExtra("FromType", FromType);
        return intent;
    }

    private void parseIntent() {
        Intent intent = getIntent();
        goodsId = intent.getStringExtra("goodsId");
        logo_pic = intent.getStringExtra("logo_pic");
        FromType = intent.getStringExtra("FromType");
    }

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_detail_f);
        ButterKnife.bind(this);
        parseIntent();
        actionbar.setData("商品详情", R.drawable.ic_left_back, null, R.drawable.ic_xiangqing_share, "分享", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //改变状态栏颜色
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        if (FromType.equals("1")) {
            lay.setVisibility(View.VISIBLE);
        }
        initView();
    }

//    private void initData() {
//        showDialog("加载中");
//        SharedPreferences sp = getBaseContext().getSharedPreferences("user", Context.MODE_PRIVATE);
//        token = sp.getString("token", "");
//        Map<String, Object> map = new HashMap<>();
//        map.put("token", token);
//        map.put("goodsId", goodsId);
//
//        XUtil.Post(Url.GOODS_DETAILS, map, new MyCallBack<String>() {
//            @Override
//            public void onSuccess(String result) {
//                super.onSuccess(result);
//                LogUtils.e("==商品详情==", result);
//
//                Gson gson = new Gson();
//                goodsNewBean = gson.fromJson(result, GoodsNewBean.class);
//
//                if (goodsNewBean.getRe().equals("1")) {
//                    setData(goodsNewBean.getInfo());
//                    //标签列表
//                    setLabel(goodsNewBean.getInfo().getLabel());
//                    //设置顶部详情图片
//                    setBanner(goodsNewBean.getInfo().getBanner());
//
//                    if (goodsNewBean.getInfo().getCollect_status().equals("1")) {
//                        isCllected = true;
//                    }
//
//                } else if (goodsNewBean.getRe().equals("0")) {
//                    ToastUtils.showToast(RebateGoodsDetailsActivity.this, "网络错误!");
//                }
//            }
//
//            @Override
//            public void onFinished() {
//                super.onFinished();
//                closeDialog();
//            }
//        });
//    }

    private void initView() {
        ll_collect.setOnClickListener(this);
        ll_buy.setOnClickListener(this);
        ll_pin.setOnClickListener(this);
        tvLook.setOnClickListener(this);
        ll_allComment.setOnClickListener(this);

        list.add("http://img5.imgtn.bdimg.com/it/u=1444023808,3753293381&fm=21&gp=0.jpg");
        list.add("http://img4.imgtn.bdimg.com/it/u=882039601,2636712663&fm=21&gp=0.jpg");
        list.add("http://img4.imgtn.bdimg.com/it/u=4119861953,350096499&fm=21&gp=0.jpg");
        list.add("http://img5.imgtn.bdimg.com/it/u=2437456944,1135705439&fm=21&gp=0.jpg");
        list.add("http://img2.imgtn.bdimg.com/it/u=3251359643,4211266111&fm=21&gp=0.jpg");


        //中间画横线
        tv_oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);


        /**
         *  商品图
         */
        recycler_view.setLayoutManager(new LinearLayoutManager(FreeGoodsDetailsActivity.this, LinearLayoutManager.HORIZONTAL, false));
        recycler_view.setAdapter(new BaseRVAdapter(FreeGoodsDetailsActivity.this, list) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_one_item_layout;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageView img = holder.getView(R.id.iv_img);
                Glide.with(FreeGoodsDetailsActivity.this).load(list.get(position)).into(img);
            }
        });


        /**
         * 评论的部分
         */
        tv_userComment.setExpand(isexpand);
        tv_userComment.setExpandStatusListener(new ExpandTextView.ExpandStatusListener() {
            @Override
            public void statusChange(boolean isExpand) {
                isexpand = isExpand;
            }
        });
        tv_userComment.setText("挺不错的,物流也可以,颜色样式都是一样一样的; 挺不错的,物流也可以,颜色样式都是一样一样的; 挺不错的,物流也可以,颜色样式都是一样一样的 —— 重要事情说三遍!");

        QuanZiImageAdapter ziImageAdapter = new QuanZiImageAdapter(FreeGoodsDetailsActivity.this, list);
        gridview.setAdapter(ziImageAdapter);


        /**
         * 商品详情页
         */
        WebSettings webSettings = webView.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //关闭webview的放大缩小按钮
        webSettings.setDisplayZoomControls(false);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLoadWithOverviewMode(false);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);// 排版适应屏幕
        // 加载链接
        webView.loadUrl("http://www.baidu.com");
        //设置Web视图
        webView.setWebViewClient(new WebClient());
    }

    public class WebClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {
        //分享
        ShareInfo shareInfo = new ShareInfo();
        shareInfo.title = "小鸟云创";
        shareInfo.text = "专注各种商品商城，优惠多多！";
        shareInfo.targetUrl = "http://www.baidu.com";

        shareDialog = new ShareDialog(FreeGoodsDetailsActivity.this, shareInfo, null, new UMShareListener() {
            @Override
            public void onResult(SHARE_MEDIA platform) {
                Toast.makeText(FreeGoodsDetailsActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                Toast.makeText(FreeGoodsDetailsActivity.this, "分享失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(SHARE_MEDIA platform) {
                Toast.makeText(FreeGoodsDetailsActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
            }
        });
        shareDialog.show();
    }

    @Override
    public void onEditTextClick() {

    }

//    public void setData(GoodsNewBean.InfoBean infoBean) {
//        LogUtils.e("==商品详情==", "进来了3");
//        tv_title.setText(infoBean.getGoods_name());
//
//        tv_price.setText("¥ " + infoBean.getPrice());
//        tv_collect.setText("收藏：" + infoBean.getCollection());
//        tv_sales.setText("月销：" + infoBean.getSales() + "笔");
//        tv_city.setText("地址：" + infoBean.getConsignment());
//
//        weight = infoBean.getWeight();
//        //有无评论的显示
//        if (infoBean.getCommentCount().equals("0")) {
//            ll_no_comment.setVisibility(View.VISIBLE);
//        } else {
//            ll_have_comment.setVisibility(View.VISIBLE);
//
//            tv_comment.setText("宝贝评价(" + infoBean.getCommentCount() + ")");
//            MyImageLoader.displayAvatar(infoBean.getComment().getHeadimg(), iv_userImg);
//            tv_userName.setText(infoBean.getComment().getNickname());
//            String comment = infoBean.getComment().getContent();
//            if (!TextUtils.isEmpty(comment)) {
//                tv_userComment.setExpand(isexpand);
//                tv_userComment.setExpandStatusListener(new ExpandTextView.ExpandStatusListener() {
//                    @Override
//                    public void statusChange(boolean isExpand) {
//                        isexpand = isExpand;
//                    }
//                });
//                tv_userComment.setText(comment);
//            }
//            tv_commentTime.setText(infoBean.getComment().getPosttime());
//
//        }
//
//
//        if (infoBean.getCollect_status().equals("1")) {
//            iv_collect.setBackgroundResource(R.drawable.ic_home_checked);
//            tv_collect_status.setText("已收藏");
//        }
//
//        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//        webView.getSettings().setLoadWithOverviewMode(false);
//        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);// 排版适应屏幕
//        webView.loadUrl(infoBean.getDetail());
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_allComment:
                startActivity(AllCommentActivity.createIntent(FreeGoodsDetailsActivity.this,""+goodsId,"3"));
                break;
            case R.id.ll_collect:
//                map.put("goodsId", goodsId);
//                map.put("token", token);
//
//                if (isCllected) {
//                    map.put("type", 2);
//                } else {
//                    map.put("type", 1);
//                }
//
//                XUtil.Post(Url.GOODS_COLLECT, map, new MyCallBack<String>() {
//                    @Override
//                    public void onSuccess(String result) {
//                        super.onSuccess(result);
//                        try {
//                            JSONObject jsonObject = new JSONObject(result);
//                            LogUtils.e(" ==== 收藏 ==", result);
//                            if (jsonObject.optString("re").equals("1")) {
//                                if (isCllected) {
//                                    isCllected = false;
//                                    iv_collect.setBackgroundResource(R.drawable.ic_home_uncheck);
//                                    tv_collect_status.setText("收藏");
//                                    tv_collect.setText("收藏：" + jsonObject.optString("num"));
//                                } else {
//                                    isCllected = true;
//                                    iv_collect.setBackgroundResource(R.drawable.ic_home_checked);
//                                    tv_collect_status.setText("已收藏");
//                                    tv_collect.setText("收藏：" + jsonObject.optString("num"));
//                                }
//                            }
//                            ToastUtils.showToast(RebateGoodsDetailsActivity.this, jsonObject.optString("info"));
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });

                if (isCllected) {
                    isCllected = false;
                    iv_collect.setBackgroundResource(R.drawable.ic_home_uncheck);
                    tv_collect_status.setText("收藏");
                } else {
                    isCllected = true;
                    iv_collect.setBackgroundResource(R.drawable.ic_home_checked);
                    tv_collect_status.setText("已收藏");
                }
                ToastUtils.showToast(FreeGoodsDetailsActivity.this, tv_collect_status.getText().toString());
                break;
            case R.id.ll_buy:
                startActivity(AddCartActivity.createIntent(FreeGoodsDetailsActivity.this, goodsId,"" ,logo_pic,""));
                break;
            case R.id.ll_pin:
                startActivity(AddCartActivity.createIntent(FreeGoodsDetailsActivity.this, goodsId,"" ,logo_pic,""));
                break;
            case R.id.tv_look:
                startActivity(StoreActivity.createIntent(this));
                break;
        }
    }
}
